package nc.impl.hrpbm.report.budgetmakedetail;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.codeplatform.framework.service.ServiceSupport;
import nc.hrpbm.report.budgetmakedetail.BudgetmakedetailService;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.pub.hrpbm.util.HrpbmReportUtils;
import nc.pub.smart.context.SmartContext;
import nc.pub.smart.data.DataSet;
import nc.vo.hrpbm.budgetmakedetail.BudgetmakedetailFiled;
import nc.vo.hrpbm.budgetmakedetail.budgetmakedetailreportVO;
import nc.vo.pub.BusinessException;
import nc.pub.smart.metadata.MetaData;
import nccloud.commons.lang.StringUtils;
import org.apache.commons.collections4.CollectionUtils;

public class BudgetmakedetailServiceImpl
		extends ServiceSupport
		implements BudgetmakedetailService{
	// 查询项目信息的SQL
	public static final String QUERY_SQL =
					"SELECT bb.pk_project as projectpk,\n" +
					"       bb.project_name as projectname,\n" +
					"       bb.pk_parentpro as parentproject,\n" +
					"       dd.name as deptname,\n" +
					"       dd.pk_dept as pk_dept,\n" +
					"       ee.code as inoutclasscode,\n" +
					"       ee.name as inoutclassname,\n" +
					"       ee.pk_inoutbusiclass as pk_inoutclass,\n" +
					"       ee.def1 as inoutlevel,\n" +
					"       CASE\n" +
					"         WHEN hbm.approvestatus = '1' THEN\n" +
					"          ff.code\n" +
					"         ELSE\n" +
					"          gg.code\n" +
					"       END AS fundsourcecode,\n" +
					"       CASE\n" +
					"         WHEN hbm.approvestatus = '1' THEN\n" +
					"          hbrd.amount_firstup\n" +
					"         ELSE\n" +
					"          hbmd.amount_firstup\n" +
					"       END AS firstup,\n" +
					"       CASE\n" +
					"         WHEN hbm.approvestatus = '1' THEN\n" +
					"          hbrd.amount_firstdown\n" +
					"         ELSE\n" +
					"          hbmd.amount_firstdown\n" +
					"       END AS firstdown,\n" +
					"       hbmd.memo def3,\n" +
					"       hbrd.firstdown_memo def4\n" +
					"  FROM hrpbm_budgetmake hbm\n" +
					"  LEFT JOIN hrpbm_budgetmakedetail hbmd\n" +
					"    ON hbmd.pk_budgetmake = hbm.pk_budgetmake\n" +
					"  LEFT JOIN bd_fundsource ff\n" +
					"    ON hbmd.pk_fundsource = ff.pk_fundsource\n" +
					"  LEFT JOIN hrpbm_budgetreply hbr\n" +
					"    ON hbr.pk_firstupbill = hbm.pk_budgetmake\n" +
					"  LEFT JOIN hrpbm_budgetreplydetail hbrd\n" +
					"    ON hbrd.pk_budgetreply = hbr.pk_budgetreply\n" +
					"   AND hbrd.pk_firstupdetail = hbmd.pk_budgetmakedetail\n" +
					"  LEFT JOIN bd_fundsource gg\n" +
					"    ON hbrd.pk_fundsource = gg.pk_fundsource\n" +
					"  LEFT JOIN org_dept dd\n" +
					"    ON hbmd.pk_budgetdept = dd.pk_dept\n" +
					"  LEFT JOIN bd_project bb\n" +
					"    ON hbmd.pk_project = bb.pk_project\n" +
					"  LEFT JOIN bd_inoutbusiclass ee\n" +
					"    ON hbmd.pk_inoutclass = ee.pk_inoutbusiclass\n" +
					"  LEFT JOIN (SELECT DISTINCT bd_defdoc.code             AS pk_fund,\n" +
					"                             fip_docview_b.factorvalue5 AS pk_inoutbusiclass\n" +
					"               FROM fip_docview_b\n" +
					"              INNER JOIN fip_docview\n" +
					"                 ON fip_docview_b.pk_classview = fip_docview.pk_classview\n" +
					"              INNER JOIN bd_defdoc\n" +
					"                 ON fip_docview_b.factorvalue3 = bd_defdoc.pk_defdoc\n" +
					"              WHERE fip_docview.viewcode = 'YS001') fip_docview_b\n" +
					"    ON hbmd.pk_inoutclass = fip_docview_b.pk_inoutbusiclass\n"+
					" WHERE ~ \n" + // ~ 替换sql语句
					" UNION ALL \n" +  // 拼接批复新增明细数据
					"SELECT f.pk_project as projectpk,\n" +
					"       f.project_name as projectname,\n" +
					"       f.pk_parentpro as parentproject,\n" +
					"       e.name as deptname,\n" +
					"       e.pk_dept as pk_dept,\n" +
					"       g.code as inoutclasscode,\n" +
					"       g.name as inoutclassname,\n" +
					"       g.pk_inoutbusiclass as pk_inoutclass,\n" +
					"       g.def1 as inoutlevel,\n" +
					"       d.code AS fundsourcecode,\n" +
					"       b.amount_firstup AS firstup,\n" +
					"       b.amount_firstdown AS firstdown,\n" +
					"       '' AS def3,\n" +
					"       a.memo AS def4\n" +
					"  FROM hrpbm_budgetreply a\n" +
					"  LEFT JOIN hrpbm_budgetreplydetail b\n" +
					"    ON b.pk_budgetreply = a.pk_budgetreply\n" +
					"  LEFT JOIN hrpbm_budgetmakedetail c\n" +
					"    ON c.pk_budgetmakedetail = b.pk_firstupdetail\n" +
					"  LEFT JOIN bd_fundsource d\n" +
					"    ON b.pk_fundsource = d.pk_fundsource\n" +
					"  LEFT JOIN org_dept e\n" +
					"    ON b.pk_budgetdept = e.pk_dept\n" +
					"  LEFT JOIN bd_project f\n" +
					"    ON b.pk_project = f.pk_project\n" +
					"  LEFT JOIN bd_inoutbusiclass g\n" +
					"    ON b.pk_inoutclass = g.pk_inoutbusiclass\n" +
					" WHERE # \n" + // # 替换sql语句
					"   AND c.pk_budgetmakedetail is NULL \n" +
					" ORDER BY inoutclasscode ";

	private BaseDAO dao;

	private BaseDAO getDAO() {
		if (dao == null) {
			dao = new BaseDAO();
		}
		return dao;
	}
	@Override
	public DataSet queryData(SmartContext paramSmartContext) throws BusinessException {
		HrpbmReportUtils.handelContext(paramSmartContext);
		budgetmakedetailreportVO[] vos = null;
		try {
			vos = this.getProcessedData(paramSmartContext);
		} catch (ParseException e) {

			e.printStackTrace();
		}
		MetaData metadata = HrpbmReportUtils.getMetaData(budgetmakedetailreportVO.getFieldTypes(),
				budgetmakedetailreportVO.getFields());
		metadata.getField("sproject").setPrecision(400);

		metadata.getField("otherdef3").setPrecision(4000);
		metadata.getField("basedef3").setPrecision(4000);
		metadata.getField("projectdef3").setPrecision(4000);
		metadata.getField("kjjfdef3").setPrecision(4000);
		metadata.getField("ftczdef3").setPrecision(4000);

		metadata.getField("otherdef4").setPrecision(4000);
		metadata.getField("basedef4").setPrecision(4000);
		metadata.getField("projectdef4").setPrecision(4000);
		metadata.getField("kjjfdef4").setPrecision(4000);
		metadata.getField("ftczdef4").setPrecision(4000);
		DataSet ds = HrpbmReportUtils.buildDataSet(metadata, vos, budgetmakedetailreportVO.getFields());
		return ds;
	}

	private budgetmakedetailreportVO[] getProcessedData(SmartContext context) throws BusinessException, ParseException {
		List<budgetmakedetailreportVO> dataList = new ArrayList<>();
		// 获取查询参数
		String pk_org = (String) context.getAttribute("pkorg");
		String budgetdept = (String) context.getAttribute("pk_budgetdept");
		String inoutlevel = (String) context.getAttribute("inoutlevel");
//		String pk_inoutclass = (String) context.getAttribute("pk_inoutclass");
		String nyear = (String) context.getAttribute("nyear");
		String iszj = (String) context.getAttribute("iszj");
		String queryProinfos = QUERY_SQL;
		String wheresql ="hbmd.PK_ORG = '"+pk_org+"' AND hbmd.PK_BUDGETTYPE = 'YS02' ";
		String unionsql ="a.PK_ORG = '"+pk_org+"' AND b.PK_BUDGETTYPE = 'YS02' ";
//		if(budgetdept != null) {
//			wheresql += "AND cc.pk_budgetdept = '"+budgetdept+"' ";
//		}
		if(nyear != null) {
			wheresql += " AND hbmd.nyear = '"+nyear+"' ";
			unionsql += " AND a.nyear = '"+nyear+"' ";
		}
//		if(pk_inoutclass != null) {
//			wheresql += "AND ee.PK_INOUTBUSICLASS = '"+pk_inoutclass+"' ";
//		}
		if(iszj != null){
			wheresql += "AND fip_docview_b.pk_fund = '"+ iszj +"'  ";
		}
		queryProinfos = queryProinfos.replace("~", wheresql);
		queryProinfos = queryProinfos.replace("#", unionsql);// 批复新增数据查询条件
		queryDataByCond(dataList,queryProinfos,pk_org,inoutlevel,nyear,budgetdept,iszj);
		return dataList.toArray(new budgetmakedetailreportVO[dataList.size()]);
	}


	private void queryDataByCond(List<budgetmakedetailreportVO> dataList, String queryProinfos,String pk_org,String inoutlevel,String nyear,String budgetdept,String iszj) throws BusinessException, ParseException {
		// TODO Auto-generated method stub
		List<BudgetmakedetailFiled> budgetmakedetailList = (List<BudgetmakedetailFiled>) getDAO()
				.executeQuery(queryProinfos, new BeanListProcessor(BudgetmakedetailFiled.class));
		Map<String, List<BudgetmakedetailFiled>> groupByVpurbillcodeMap = new HashMap<>();
		List<String> idList = new ArrayList();
		Map<String,budgetmakedetailreportVO> resultTempMap  = new HashMap();
		//处理收支项目级次问题
		List<String> parentList = new ArrayList();
		Map<String,budgetmakedetailreportVO> parentTempMap  = new HashMap();
		Map<String,List> parenMetaMap  = new HashMap();

		for (BudgetmakedetailFiled dateVo : budgetmakedetailList) {
			String id = dateVo.getProjectname()+dateVo.getDeptname()+dateVo.getInoutclasscode();
			if(idList.contains(id)) {
				budgetmakedetailreportVO vo  = resultTempMap.get(id);
				if(dateVo.getFirstup()!=null) {
					if(vo.getFirstuptotal()!=null) {
						vo.setFirstuptotal(vo.getFirstuptotal().add(dateVo.getFirstup()));
					}else {
						vo.setFirstuptotal(dateVo.getFirstup());
					}
				}
				if(dateVo.getFirstdown()!=null) {
					if(vo.getFirstdowntotal()!=null) {
						vo.setFirstdowntotal(vo.getFirstdowntotal().add(dateVo.getFirstdown()));
					}else {
						vo.setFirstdowntotal(dateVo.getFirstdown());
					}
				}

				if("01".equals(dateVo.getFundsourcecode())) {
					if(dateVo.getFirstup()!=null) {
						if(vo.getBasefirstup()!=null) {
							vo.setBasefirstup(vo.getBasefirstup().add(dateVo.getFirstup()));
						}else {
							vo.setBasefirstup(dateVo.getFirstup());
						}
					}
					if(dateVo.getFirstdown()!=null) {
						if(vo.getBasefirstdown()!=null) {
							vo.setBasefirstdown(vo.getBasefirstdown().add(dateVo.getFirstdown()));
						}else {
							vo.setBasefirstdown(dateVo.getFirstdown());
						}
					}
					vo.setBasedef3(dateVo.getDef3());
					vo.setBasedef4(dateVo.getDef4());
				}else if("02".equals(dateVo.getFundsourcecode())) {
					if(dateVo.getFirstup()!=null) {
						if(vo.getProjectfirstup()!=null) {
							vo.setProjectfirstup(vo.getProjectfirstup().add(dateVo.getFirstup()));
						}else {
							vo.setProjectfirstup(dateVo.getFirstup());
						}
					}

					if(dateVo.getFirstdown()!=null) {
						if(vo.getProjectfirstdown()!=null) {
							vo.setProjectfirstdown(vo.getProjectfirstdown().add(dateVo.getFirstdown()));
						}else {
							vo.setProjectfirstdown(dateVo.getFirstdown());
						}
					}

					vo.setProjectdef3(dateVo.getDef3());
					vo.setProjectdef4(dateVo.getDef4());
				}else if("03".equals(dateVo.getFundsourcecode())) {
					if(dateVo.getFirstup()!=null) {
						if(vo.getKjjffirstup()!=null) {
							vo.setKjjffirstup(vo.getKjjffirstup().add(dateVo.getFirstup()));
						}else {
							vo.setKjjffirstup(dateVo.getFirstup());
						}
					}
					if(dateVo.getFirstdown()!=null) {
						if(vo.getKjjffirstdown()!=null) {
							vo.setKjjffirstdown(vo.getKjjffirstdown().add(dateVo.getFirstdown()));
						}else {
							vo.setKjjffirstdown(dateVo.getFirstdown());
						}
					}

					vo.setKjjfdef3(dateVo.getDef3());
					vo.setKjjfdef4(dateVo.getDef4());
				}else if("04".equals(dateVo.getFundsourcecode())) {
					if(dateVo.getFirstup()!=null) {
						if(vo.getFtczfirstup()!=null) {
							vo.setFtczfirstup(vo.getFtczfirstup().add(dateVo.getFirstup()));
						}else {
							vo.setFtczfirstup(dateVo.getFirstup());
						}
					}
					if(dateVo.getFirstdown()!=null) {
						if(vo.getFtczfirstdown()!=null) {
							vo.setFtczfirstdown(vo.getFtczfirstdown().add(dateVo.getFirstdown()));
						}else {
							vo.setFtczfirstdown(dateVo.getFirstdown());
						}
					}

					vo.setFtczdef3(dateVo.getDef3());
					vo.setFtczdef4(dateVo.getDef4());
				}else if("05".equals(dateVo.getFundsourcecode())) {
					if(dateVo.getFirstup()!=null) {
						if(vo.getOtherfirstup()!=null) {
							vo.setOtherfirstup(vo.getOtherfirstup().add(dateVo.getFirstup()));
						}else {
							vo.setOtherfirstup(dateVo.getFirstup());
						}
					}
					if(dateVo.getFirstdown()!=null) {
						if(vo.getOtherfirstdown()!=null) {
							vo.setOtherfirstdown(vo.getOtherfirstdown().add(dateVo.getFirstdown()));
						}else {
							vo.setOtherfirstdown(dateVo.getFirstdown());
						}
					}
					vo.setOtherdef3(dateVo.getDef3());
					vo.setOtherdef4(dateVo.getDef4());
				}
			}else {
				budgetmakedetailreportVO vo = new budgetmakedetailreportVO();
				if(dateVo.getParentproject() != null && dateVo.getParentproject() != "") {
					//获取一级项目名称
					String  projectSql = "SELECT PROJECT_NAME FROM BD_PROJECT WHERE PK_PROJECT = '"+dateVo.getParentproject()+"'";
					String parentname = (String) dao.executeQuery(projectSql, new ColumnProcessor());
					vo.setPkfproject(dateVo.getParentproject());
					vo.setFproject(parentname);
				}
				vo.setPksproject(dateVo.getProjectpk());
				vo.setSproject(dateVo.getProjectname());
				vo.setBudgetdept(dateVo.getDeptname());
				vo.setInoutlevel(dateVo.getInoutlevel());
				vo.setInoutclasscode(dateVo.getInoutclasscode());
				vo.setInoutclassname(dateVo.getInoutclassname());
				vo.setFirstuptotal(dateVo.getFirstup());
				vo.setFirstdowntotal(dateVo.getFirstdown());
				vo.setPkorg(pk_org);
				vo.setPk_budgetdept(dateVo.getPk_dept());
				vo.setPk_inoutclass(dateVo.getPk_inoutclass());
				vo.setIszj(qyiszj(dateVo.getPk_inoutclass()));
				vo.setNyear(nyear);
				//财政基本拨款经费 01  财政项目拨款经费 02  科教经费 03 非同级财政拨款经费 04 其他经费05
				if("01".equals(dateVo.getFundsourcecode())) {
					vo.setBasefirstdown(dateVo.getFirstdown());
					vo.setBasefirstup(dateVo.getFirstup());
					vo.setBasedef3(dateVo.getDef3());
					vo.setBasedef4(dateVo.getDef4());
				}else if("02".equals(dateVo.getFundsourcecode())) {
					vo.setProjectfirstdown(dateVo.getFirstdown());
					vo.setProjectfirstup(dateVo.getFirstup());
					vo.setProjectdef3(dateVo.getDef3());
					vo.setProjectdef4(dateVo.getDef4());
				}else if("03".equals(dateVo.getFundsourcecode())) {
					vo.setKjjffirstdown(dateVo.getFirstdown());
					vo.setKjjffirstup(dateVo.getFirstup());
					vo.setKjjfdef3(dateVo.getDef3());
					vo.setKjjfdef4(dateVo.getDef4());
				}else if("04".equals(dateVo.getFundsourcecode())) {
					vo.setFtczfirstdown(dateVo.getFirstdown());
					vo.setFtczfirstup(dateVo.getFirstup());
					vo.setFtczdef3(dateVo.getDef3());
					vo.setFtczdef4(dateVo.getDef4());
				}else if("05".equals(dateVo.getFundsourcecode())) {
					vo.setOtherfirstdown(dateVo.getFirstdown());
					vo.setOtherfirstup(dateVo.getFirstup());
					vo.setOtherdef3(dateVo.getDef3());
					vo.setOtherdef4(dateVo.getDef4());
				}
				idList.add(id);
				resultTempMap.put(id, vo);
			}
		}
		Map<String,budgetmakedetailreportVO> codeTempMap  = new HashMap();
		//处理级次
		for(String id : idList) {
			budgetmakedetailreportVO vo = resultTempMap.get(id);
			if(vo.getInoutlevel()!=1) {
				String childcode = vo.getInoutclasscode();
				int level = vo.getInoutlevel();
//				if(!parenMetaMap.containsKey(childcode)) {
				queryParentVo(parentList,parentTempMap,parenMetaMap,childcode,level,vo,pk_org,inoutlevel,nyear);
//				}
				//处理收支项目集合
				if(codeTempMap.containsKey(childcode)) {
					budgetmakedetailreportVO codevo = codeTempMap.get(childcode);
					if(vo.getFirstuptotal()!=null) {
						if(codevo.getFirstuptotal()!=null) {
							codevo.setFirstuptotal(codevo.getFirstuptotal().add(vo.getFirstuptotal()));
						}else {
							codevo.setFirstuptotal(vo.getFirstuptotal());
						}
					}
					if(vo.getFirstdowntotal()!=null) {
						if(codevo.getFirstdowntotal()!=null) {
							codevo.setFirstdowntotal(codevo.getFirstdowntotal().add(vo.getFirstdowntotal()));
						}else {
							codevo.setFirstdowntotal(vo.getFirstdowntotal());
						}
					}

					if(vo.getBasefirstup()!=null) {
						if(codevo.getBasefirstup()!=null) {
							codevo.setBasefirstup(codevo.getBasefirstup().add(vo.getBasefirstup()));
						}else {
							codevo.setBasefirstup(vo.getBasefirstup());
						}
					}
					if(vo.getBasefirstdown()!=null) {
						if(codevo.getBasefirstdown()!=null) {
							codevo.setBasefirstdown(codevo.getBasefirstdown().add(vo.getBasefirstdown()));
						}else {
							codevo.setBasefirstdown(vo.getBasefirstdown());
						}
					}
//					codevo.setBasedef3(vo.getBasedef3());
//					codevo.setBasedef4(vo.getBasedef4());
					if(vo.getProjectfirstup()!=null) {
						if(codevo.getProjectfirstup()!=null) {
							codevo.setProjectfirstup(codevo.getProjectfirstup().add(vo.getProjectfirstup()));
						}else {
							codevo.setProjectfirstup(vo.getProjectfirstup());
						}
					}

					if(vo.getProjectfirstdown()!=null) {
						if(codevo.getProjectfirstdown()!=null) {
							codevo.setProjectfirstdown(codevo.getProjectfirstdown().add(vo.getProjectfirstdown()));
						}else {
							codevo.setProjectfirstdown(vo.getProjectfirstdown());
						}
					}

//					codevo.setProjectdef3(vo.getProjectdef3());
//					codevo.setProjectdef4(vo.getProjectdef4());
					if(vo.getKjjffirstup()!=null) {
						if(codevo.getKjjffirstup()!=null) {
							codevo.setKjjffirstup(codevo.getKjjffirstup().add(vo.getKjjffirstup()));
						}else {
							codevo.setKjjffirstup(vo.getKjjffirstup());
						}
					}
					if(vo.getKjjffirstdown()!=null) {
						if(codevo.getKjjffirstdown()!=null) {
							codevo.setKjjffirstdown(codevo.getKjjffirstdown().add(vo.getKjjffirstdown()));
						}else {
							codevo.setKjjffirstdown(vo.getKjjffirstdown());
						}
					}

//					codevo.setKjjfdef3(vo.getKjjfdef3());
//					codevo.setKjjfdef4(vo.getKjjfdef4());
					if(vo.getFtczfirstup()!=null) {
						if(codevo.getFtczfirstup()!=null) {
							codevo.setFtczfirstup(codevo.getFtczfirstup().add(vo.getFtczfirstup()));
						}else {
							codevo.setFtczfirstup(codevo.getFtczfirstup());
						}
					}
					if(vo.getFtczfirstdown()!=null) {
						if(codevo.getFtczfirstdown()!=null) {
							codevo.setFtczfirstdown(codevo.getFtczfirstdown().add(vo.getFtczfirstdown()));
						}else {
							codevo.setFtczfirstdown(vo.getFtczfirstdown());
						}
					}

//					codevo.setFtczdef3(vo.getFtczdef3());
//					codevo.setFtczdef4(vo.getFtczdef4());
					if(vo.getOtherfirstup()!=null) {
						if(codevo.getOtherfirstup()!=null) {
							codevo.setOtherfirstup(codevo.getOtherfirstup().add(vo.getOtherfirstup()));
						}else {
							codevo.setOtherfirstup(vo.getOtherfirstup());
						}
					}
					if(vo.getOtherfirstdown()!=null) {
						if(codevo.getOtherfirstdown()!=null) {
							codevo.setOtherfirstdown(codevo.getOtherfirstdown().add(vo.getOtherfirstdown()));
						}else {
							codevo.setOtherfirstdown(vo.getOtherfirstdown());
						}
					}
//					codevo.setOtherdef3(vo.getOtherdef3());
//					codevo.setOtherdef4(vo.getOtherdef4());
				}else {
					budgetmakedetailreportVO codevo = new budgetmakedetailreportVO();
					codevo.setInoutlevel(vo.getInoutlevel());
					codevo.setInoutclasscode(vo.getInoutclasscode());
					codevo.setInoutclassname(vo.getInoutclassname());
					codevo.setFirstuptotal(vo.getFirstuptotal());
					codevo.setFirstdowntotal(vo.getFirstdowntotal());
					codevo.setPkorg(pk_org);
					codevo.setPk_inoutclass(vo.getPk_inoutclass());
					codevo.setIszj(qyiszj(vo.getPk_inoutclass()));
					codevo.setNyear(nyear);
					codevo.setBasefirstdown(vo.getBasefirstdown());
					codevo.setBasefirstup(vo.getBasefirstup());
//					codevo.setBasedef3(vo.getBasedef3());
//					codevo.setBasedef4(vo.getBasedef4());
					codevo.setProjectfirstdown(vo.getProjectfirstdown());
					codevo.setProjectfirstup(vo.getProjectfirstup());
//					codevo.setProjectdef3(vo.getProjectdef3());
//					codevo.setProjectdef4(vo.getProjectdef4());
					codevo.setKjjffirstdown(vo.getKjjffirstdown());
					codevo.setKjjffirstup(vo.getKjjffirstup());
//					codevo.setKjjfdef3(vo.getKjjfdef3());
//					codevo.setKjjfdef4(vo.getKjjfdef4());
					codevo.setFtczfirstdown(vo.getFtczfirstdown());
					codevo.setFtczfirstup(vo.getFtczfirstup());
//					codevo.setFtczdef3(vo.getFtczdef3());
//					codevo.setFtczdef4(vo.getFtczdef4());
					codevo.setOtherfirstdown(vo.getOtherfirstdown());
					codevo.setOtherfirstup(vo.getOtherfirstup());
//					codevo.setOtherdef3(vo.getOtherdef3());
//					codevo.setOtherdef4(vo.getOtherdef4());
					codeTempMap.put(childcode, codevo);
				}
			}
		}
		List<String> readyList = new ArrayList();
		List<String> codereadyList = new ArrayList();
		//增加合计
		BigDecimal firstuptotal = new BigDecimal(0);
		BigDecimal firstdowntotal = new BigDecimal(0);
		BigDecimal basefirstdowntotal = new BigDecimal(0);
		BigDecimal basefirstuptotal = new BigDecimal(0);
		BigDecimal projectfirstdowntotal = new BigDecimal(0);
		BigDecimal projectfirstuptotal = new BigDecimal(0);
		BigDecimal kjjffirstdowntotal = new BigDecimal(0);
		BigDecimal kjjffirstuptotal = new BigDecimal(0);
		BigDecimal ftczfirstdowntotal = new BigDecimal(0);
		BigDecimal ftczfirstuptotal = new BigDecimal(0);
		BigDecimal otherfirstdowntotal = new BigDecimal(0);
		BigDecimal otherfirstuptotal = new BigDecimal(0);
		//创建科室集合
		List<String> budgetdeptList = new ArrayList();
		if(budgetdept != null) {
			if(budgetdept.contains(",")) {
				String[] budgetdeptstr = budgetdept.split(",");
                Collections.addAll(budgetdeptList, budgetdeptstr);
			}else {
				budgetdeptList.add(budgetdept);
			}
		}
		//处理结果集
		for(String id : idList) {
			budgetmakedetailreportVO vo = resultTempMap.get(id);
			String currcode = vo.getInoutclasscode();
			//获取上级codelist
			List<String> pcodelist= parenMetaMap.get(currcode);
			if(CollectionUtils.isNotEmpty(pcodelist)){
				for(int i = pcodelist.size();i>0;i--) {
					String pcode = pcodelist.get(i-1);
					if(!readyList.contains(pcode)) {
						dataList.add(parentTempMap.get(pcode));
						readyList.add(pcode);
					}
				}
			}
			if(!codereadyList.contains(currcode)) {
				dataList.add(codeTempMap.get(currcode));
				codereadyList.add(currcode);
			}
			dataList.add(vo);
			if((budgetdeptList.size() == 0 || budgetdeptList.contains(vo.getPk_budgetdept())) && (iszj == null || iszj.equals(vo.getIszj()))) {
				//处理合计
				if(vo.getFirstuptotal()!=null) {
					firstuptotal = firstuptotal.add(vo.getFirstuptotal());
				}
				if(vo.getFirstdowntotal()!=null) {
					firstdowntotal = firstdowntotal.add(vo.getFirstdowntotal());
				}
				if(vo.getBasefirstup()!=null) {
					basefirstuptotal = basefirstuptotal.add(vo.getBasefirstup());
				}
				if(vo.getBasefirstdown()!=null) {
					basefirstdowntotal = basefirstdowntotal.add(vo.getBasefirstdown());
				}
				if(vo.getProjectfirstup()!=null) {
					projectfirstuptotal = projectfirstuptotal.add(vo.getProjectfirstup());
				}
				if(vo.getProjectfirstdown()!=null) {
					projectfirstdowntotal = projectfirstdowntotal.add(vo.getProjectfirstdown());
				}
				if(vo.getKjjffirstup()!=null) {
					kjjffirstuptotal = kjjffirstuptotal.add(vo.getKjjffirstup());
				}
				if(vo.getKjjffirstdown()!=null) {
					kjjffirstdowntotal = kjjffirstdowntotal.add(vo.getKjjffirstdown());
				}
				if(vo.getFtczfirstup()!=null) {
					ftczfirstuptotal = ftczfirstuptotal.add(vo.getFtczfirstup());
				}
				if(vo.getFtczfirstdown()!=null) {
					ftczfirstdowntotal = ftczfirstdowntotal.add(vo.getFtczfirstdown());
				}
				if(vo.getOtherfirstup()!=null) {
					otherfirstuptotal = otherfirstuptotal.add(vo.getOtherfirstup());
				}
				if(vo.getOtherfirstdown()!=null) {
					otherfirstdowntotal = otherfirstdowntotal.add(vo.getOtherfirstdown());
				}
			}
		}

		//增加合计行
		budgetmakedetailreportVO hjvo = new budgetmakedetailreportVO();
		hjvo.setInoutclassname("小计");
		hjvo.setFirstuptotal(firstuptotal);
		hjvo.setFirstdowntotal(firstdowntotal);
		hjvo.setBasefirstup(basefirstuptotal);
		hjvo.setBasefirstdown(basefirstdowntotal);
		hjvo.setProjectfirstup(projectfirstuptotal);
		hjvo.setProjectfirstdown(projectfirstdowntotal);
		hjvo.setKjjffirstup(kjjffirstuptotal);
		hjvo.setKjjffirstdown(kjjffirstdowntotal);
		hjvo.setFtczfirstup(ftczfirstuptotal);
		hjvo.setFtczfirstdown(ftczfirstdowntotal);
		hjvo.setOtherfirstup(otherfirstuptotal);
		hjvo.setOtherfirstdown(otherfirstdowntotal);
		hjvo.setPkorg(pk_org);
		hjvo.setNyear(nyear);
		if(budgetdeptList.size() > 0) {
			hjvo.setPk_budgetdept(budgetdeptList.get(0));
		}
		hjvo.setIszj(iszj);
		dataList.add(hjvo);
	}

	private void queryParentVo (List<String> parentList,Map<String,budgetmakedetailreportVO> parentTempMap,Map<String,List> parenMetaMap,String childcode,int level,budgetmakedetailreportVO dateVo,String pk_org,String inoutlevel,String nyear) throws DAOException {
		//通过级次和code递归获得上级code
		String currcode = childcode;
		for(int i =1 ;i<level;i++) {

			String parentsql =  "SELECT" +
					"	CODE,NAME,PK_INOUTBUSICLASS" +
					" FROM" +
					"	BD_INOUTBUSICLASS" +
					"	where " +
					"	PK_INOUTBUSICLASS = (SELECT PK_PARENT FROM BD_INOUTBUSICLASS WHERE CODE = '"+currcode+"')";
			Map parentMap = (Map) dao.executeQuery(parentsql, new MapProcessor());
			String parentCode= (String) parentMap.get("code");
			String parentName= (String) parentMap.get("name");
			String parentpk= (String) parentMap.get("pk_inoutbusiclass");
			if(parentList.contains(parentCode)) {
				budgetmakedetailreportVO parentVo = parentTempMap.get(parentCode);
				if(dateVo.getFirstuptotal()!=null) {
					if(parentVo.getFirstuptotal()!=null) {
						parentVo.setFirstuptotal(parentVo.getFirstuptotal().add(dateVo.getFirstuptotal()));
					}else {
						parentVo.setFirstuptotal(dateVo.getFirstuptotal());
					}
				}
				if(dateVo.getFirstdowntotal()!=null) {
					if(parentVo.getFirstdowntotal()!=null) {
						parentVo.setFirstdowntotal(parentVo.getFirstdowntotal().add(dateVo.getFirstdowntotal()));
					}else {
						parentVo.setFirstdowntotal(dateVo.getFirstdowntotal());
					}
				}

				if(dateVo.getBasefirstup()!=null) {
					if(parentVo.getBasefirstup()!=null) {
						parentVo.setBasefirstup(parentVo.getBasefirstup().add(dateVo.getBasefirstup()));
					}else {
						parentVo.setBasefirstup(dateVo.getBasefirstup());
					}
				}
				if(dateVo.getBasefirstdown()!=null) {
					if(parentVo.getBasefirstdown()!=null) {
						parentVo.setBasefirstdown(parentVo.getBasefirstdown().add(dateVo.getBasefirstdown()));
					}else {
						parentVo.setBasefirstdown(dateVo.getBasefirstdown());
					}
				}
				if(dateVo.getProjectfirstup()!=null) {
					if(parentVo.getProjectfirstup()!=null) {
						parentVo.setProjectfirstup(parentVo.getProjectfirstup().add(dateVo.getProjectfirstup()));
					}else {
						parentVo.setProjectfirstup(dateVo.getProjectfirstup());
					}
				}
				if(dateVo.getProjectfirstdown()!=null) {
					if(parentVo.getProjectfirstdown()!=null) {
						parentVo.setProjectfirstdown(parentVo.getProjectfirstdown().add(dateVo.getProjectfirstdown()));
					}else {
						parentVo.setProjectfirstdown(dateVo.getProjectfirstdown());
					}
				}
				if(dateVo.getKjjffirstup()!=null) {
					if(parentVo.getKjjffirstup()!=null) {
						parentVo.setKjjffirstup(parentVo.getKjjffirstup().add(dateVo.getKjjffirstup()));
					}else {
						parentVo.setKjjffirstup(dateVo.getKjjffirstup());
					}
				}
				if(dateVo.getKjjffirstdown()!=null) {
					if(parentVo.getKjjffirstdown()!=null) {
						parentVo.setKjjffirstdown(parentVo.getKjjffirstdown().add(dateVo.getKjjffirstdown()));
					}else {
						parentVo.setKjjffirstdown(dateVo.getKjjffirstdown());
					}
				}
				if(dateVo.getFtczfirstup()!=null) {
					if(parentVo.getFtczfirstup()!=null) {
						parentVo.setFtczfirstup(parentVo.getFtczfirstup().add(dateVo.getFtczfirstup()));
					}else {
						parentVo.setFtczfirstup(dateVo.getFtczfirstup());
					}
				}
				if(dateVo.getFtczfirstdown()!=null) {
					if(parentVo.getFtczfirstdown()!=null) {
						parentVo.setFtczfirstdown(parentVo.getFtczfirstdown().add(dateVo.getFtczfirstdown()));
					}else {
						parentVo.setFtczfirstdown(dateVo.getFtczfirstdown());
					}
				}
				if(dateVo.getOtherfirstup()!=null) {
					if(parentVo.getOtherfirstup()!=null) {
						parentVo.setOtherfirstup(parentVo.getOtherfirstup().add(dateVo.getOtherfirstup()));
					}else {
						parentVo.setOtherfirstup(dateVo.getOtherfirstup());
					}
				}
				if(dateVo.getOtherfirstdown()!=null) {
					if(parentVo.getOtherfirstdown()!=null) {
						parentVo.setOtherfirstdown(parentVo.getOtherfirstdown().add(dateVo.getOtherfirstdown()));
					}else {
						parentVo.setOtherfirstdown(dateVo.getOtherfirstdown());
					}
				}
			}else {
				budgetmakedetailreportVO parentVo = new budgetmakedetailreportVO();
				parentVo.setInoutclasscode(parentCode);
				parentVo.setInoutclassname(parentName);
				parentVo.setFirstuptotal(dateVo.getFirstuptotal());
				parentVo.setFirstdowntotal(dateVo.getFirstdowntotal());
				parentVo.setBasefirstup(dateVo.getBasefirstup());
				parentVo.setBasefirstdown(dateVo.getBasefirstdown());
				parentVo.setProjectfirstdown(dateVo.getProjectfirstdown());
				parentVo.setProjectfirstup(dateVo.getProjectfirstup());
				parentVo.setKjjffirstdown(dateVo.getKjjffirstdown());
				parentVo.setKjjffirstup(dateVo.getKjjffirstup());
				parentVo.setFtczfirstdown(dateVo.getFtczfirstdown());
				parentVo.setFtczfirstup(dateVo.getFtczfirstup());
				parentVo.setOtherfirstdown(dateVo.getOtherfirstdown());
				parentVo.setOtherfirstup(dateVo.getOtherfirstup());
				parentVo.setPkorg(pk_org);
				parentVo.setInoutlevel(level-i);
				parentVo.setPk_inoutclass(dateVo.getPk_inoutclass());
				parentVo.setIszj(qyiszj(parentpk));
				parentVo.setNyear(nyear);
				parentList.add(parentCode);
				parentTempMap.put(parentCode, parentVo);
			}
			if(parenMetaMap.containsKey(childcode)) {
				List list = parenMetaMap.get(childcode);
				list.add(parentCode);
			}else {
				List list = new ArrayList();
				list.add(parentCode);
				parenMetaMap.put(childcode, list);
			}
			currcode = parentCode;
		}
	}


	private String qyiszj (String pk_inoutclass) throws DAOException {

		String sql = "SELECT" +
				" code" +
				" FROM" +
				" BD_DEFDOC" +
				" WHERE" +
				" pk_defdoc IN ("+
				" SELECT factorvalue3 FROM FIP_DOCVIEW_B WHERE PK_CLASSVIEW = ( "+
				" SELECT PK_CLASSVIEW FROM FIP_DOCVIEW WHERE viewcode = 'YS001' ) "+
				" AND  factorvalue5 = '"+pk_inoutclass+"')";
		String code = (String) dao.executeQuery(sql, new ColumnProcessor());
//       if(code!=null && code != "") {
//    	   return code;
//       }else {
//    	   return "02";
//       }
		return code;
	}
}
